<template>
  <div class="cart-shop">
    <div class="left">
      <label>
        <!-- 不能用v-model 因为v-model是双向的 但是这儿数据是VueX的，
        他是单向数据流，改变里面的内容只能通过commit一个mutation来改变
         -->
        <input type="checkbox" name="" id="" :checked="check" @change="toggleCheck({ id })" />
        <span></span>
      </label>
      <router-link  tag="img" :to="`/detail?id=${id}`" :src="photo[0].url" alt=""></router-link>
      <div class="left-info">
        <h3>
          {{ title.slice(0,15) }}...
        </h3>
        <div class="left-price">
          <span>￥{{ price | toFix2 }}</span>
          <del>￥{{ originPrice | toFix2 }}</del>
        </div>
      </div>
    </div>
    <div class="right">
      <span>
        <van-stepper :value="count" @change="changeNum" theme="round" button-size="20" disable-input />
      </span>
    </div>
  </div>
</template>

<script>
import {
  mapMutations
} from 'vuex'
export default {
  props: {
    id: {
      type: Number,
      redirect: true
    },
    title: {
      type: String,
      redirect: true
    },
    price: {
      type: Number,
      redirect: true
    },
    photo: {
      type: Array,
      redirect: true
    },
    originPrice: {
      type: Number,
      redirect: true
    },
    count: {
      type: Number,
      redirect: true
    },
    check: {
      type: Boolean,
      redirect: true
    }
  },
  methods: {
    ...mapMutations(['toggleCheck', 'addCount']),
    changeNum (num) {
      this.$store.commit('changeNum', { id: this.id, num })
    }
  }
}
</script>

<style lang="scss" scopde>
 .cart-shop {
    display: flex;
    background: #ffffff;
    width: 100%;
    box-sizing: border-box;
    padding: 15px;
    border-radius: 10px;
    margin: 10px 0;
    .left {
      display: flex;
      align-items: center;
      // padding-right: 10px;
      img {
        width: 80px;
      }
      label {
        margin-right: 5px;
      }
      input {
        display: none;
        + span{
          display: block;
          position: relative;
          width: 16px;
          height: 16px;
          border: 1px solid #999;
          border-radius: 50%;
          margin-left: -5px;
        }
        &:checked + span {
          background-color: #1989fa;
          border-color: #1989fa;
          &::after {
            content: '';
            position: absolute;
            top: 3px;
            left: 2px;
            width: 9px;
            height: 5px;
            border: 2px solid #ffffff;
            border-width: 0 0 2px 2px;
            transform: rotate(-45deg);
          }
        }
      }
      &-info {
        margin-left: 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        height: 100%;
        h3 {
          color: #43240c;
          font-size: 14px;
        }
        .left-price {
          // margin-top: 10px;
          span:nth-child(1) {
            color: #f42;
          }
          del {
            color: #b1a9a5;
            font-size: 12px;
            padding-left: 5px;
          }
        }
      }
    }
    .right {
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      span {
        display: block;
        border: 1px solid;
        border-radius: 20px;
        box-sizing: border-box;
        border-color: #d9d9d9;
      }
      .van-stepper {
        padding: 3px;
        box-sizing: border-box;
        display: flex;
      }
    }
  }
</style>
